#include <stdio.h>  
#include <malloc.h>
#include <string.h>
#include <errno.h>
enum LEVEL
{
    BASELEVEL = 1,
    CHILDLEVE,
};

typedef struct Base
{
    int fatherData;
    // 返回值为void,参数为空的函数指针
    void (*Print)();
}Base;

// 继承了基类的子类
typedef struct Child
{
    Base base;
    int childdata;

}Child;



void BasePrint()
{
    printf("This is BasePrint\n");
}

void Child_BasePrint()
{
    printf("This is ChildPrint\n");
}

// 模拟基类构造函数
void BaseInit(Base* base, int x)
{
    base->fatherData = x;
    // 让基类的函数指针指向设置好的函数
    base->Print = BasePrint;
}

// 模拟子类构造函数
void ChildInit(Child* child, int x)
{
    // 子类的构造函数会默认先使用基类的构造函数,这里给的BaseData默认为0
    BaseInit((Base*)child, 0);
    child->childdata = x;
    // 将子类里基类的函数指针改为自己的打印方法,实现了多态
    child->base.Print = Child_BasePrint;
}

int main()
{
    Base* father = (Base*)malloc(sizeof(Base));
    BaseInit(father, 0);
    father->Print();
    printf("father data: %d\n", father->fatherData);

    printf("========================================\n");

    Base* b = (Base*)malloc(sizeof(Child));
    Child* child = b; 

    ChildInit(child, 0);
    child->base.Print();
    printf("child data: %d\n", child->childdata);

    printf("========================================\n");

    Base Testbase1;
    BaseInit(&Testbase1, 1);
    Testbase1.fatherData = 10;
    printf("Testbase1 fatherData: %d\n", Testbase1.fatherData);

    Base* Testbase2 = (Base*)malloc(sizeof(Base));
    BaseInit(Testbase2, 1);
    Testbase2->fatherData = 10;
    printf("Testbase2 fatherData: %d\n", Testbase2->fatherData);

    return 0;
}